package com.hisense.davidmoten.service.impl;

import com.hisense.davidmoten.rtree.Entry;
import com.hisense.davidmoten.rtree.RTree;
import com.hisense.davidmoten.rtree.geometry.Circle;
import com.hisense.davidmoten.rtree.geometry.Geometry;
import com.hisense.davidmoten.rtree.geometry.Point;
import com.hisense.davidmoten.rtree.geometry.Rectangle;
import com.hisense.davidmoten.service.IRTreeQuery;

import java.util.List;

/**
 * <p>@description 查询接口的实现</p>
 * @author zy_john
 * @param <T> key
 * @param <S> Geometry
 */
public class RTreeQuery<T, S extends Geometry> implements IRTreeQuery<T, S> {

    private RTree<T, S> tree;

    private RTreeQuery(RTree<T, S> tree){
        this.tree = tree;
    }
    @Override
    public List<Entry<T, S>> queryByPoint(Point point, double distant) {
        return tree.search(point, distant)
                .toList()
                .toBlocking()
                .single();
    }

    @Override
    public List<Entry<T, S>> queryByCircle(Circle circle) {
        return tree.search(circle)
                .toList()
                .toBlocking()
                .single();
    }

    @Override
    public List<Entry<T, S>> queryByRectangle(Rectangle rectangle) {
        return tree.search(rectangle)
                .toList()
                .toBlocking()
                .single();
    }

    @Override
    public List<Entry<T, S>> queryNearestByPoint(Point point, double maxDistance, int maxCount) {
        return tree.nearest(point, maxDistance, maxCount)
                .toList()
                .toBlocking()
                .single();
    }

    @Override
    public List<Entry<T, S>> queryNearestByRectangle(Rectangle r, double maxDistance, int maxCount) {
        return tree.nearest(r, maxDistance, maxCount)
                .toList()
                .toBlocking()
                .single();
    }

    public static <T, S extends Geometry> RTreeQuery<T, S> getInstance(RTree<T, S> rTree){
        return new RTreeQuery<>(rTree);
    }
}
